// component/site-monitoring/index.ts
import { getListAll } from '../../api/site-monitoring'
import type { siteList } from '../../type/type'
export interface list {
  text: String,
  value: Number | String
}
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    isOnline:{
      type:String
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    option1: [
      { text: '全部站', value: '-1' },
      { text: '热源站', value: '1' },
      { text: '换热站', value: '2' },
    ] as list[],
    option2: [
      { text: '全部', value: '0' },
      { text: '报警', value: '1' },
      { text: '正常', value: '2' },
    ] as list[],
    option3: [
      { text: '全部', value: '' },
      { text: '在线', value: '1' },
      { text: '离线', value: '0' }
    ],
    stationType: '-1' as String,
    onState: '' as String,
    type: '0' as String,
    searchstring: '' as String,
    comID: -1 as Number,
    userID: -1 as Number,
    list: [] as siteList[],
    isFresh: false as Boolean,
    intervalId: null,
    isHttp:false as Boolean
  },
  observers:{
    "stationType":function(newVal:any){
      this.getAllList();
    },
    "onState":function(newVal:any){
      this.getAllList();
    },
    "type":function(newVal:any){
      this.getAllList();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    async getAllList() {
      let type = this.data.type;
      let stationType = this.data.stationType
      let comID = this.data.comID
      let userID = this.data.userID
      let searchstring = this.data.searchstring
      let onState = this.data.onState
      const res = await getListAll(type, stationType, onState, comID, userID, searchstring)
      let count = res.data.data.filter(item => item.warnCode == 2)
      let length = count.length
      wx.setTabBarBadge({
        index: 1,
        text:String(length)
      })
      if (res.data.code == 1) {
        this.setData({
          list: res.data.data,
          isFresh: false
        })
        // console.log(this.data.list)
      }
      console.log(res)
    },
    Timertask(){
      let type = this.data.type;
      let stationType = this.data.stationType
      let comID = this.data.comID
      let userID = this.data.userID
      let searchstring = this.data.searchstring
      let onState = this.data.onState
      wx.request({
        url:'https://www.hbdinkon.cn:10020/MobileLinkCenter/equipment/selectEquipments',
        method:'POST',
        data:{
          type:0,
          stationType:stationType,
          onState:onState,
          searchString:searchstring,
          comID:comID,
          userID:userID,
        },
        header:{
          'content-type':'application/x-www-form-urlencoded'
        },
        success:(res:any)=>{
          let count = res.data.data.filter(item => item.warnCode == 2)
          let length = count.length
          wx.setTabBarBadge({
            index: 1,
            text:String(length)
          })
          if (res.data.code == 1) {
            this.setData({
              list: res.data.data,
              isFresh: false
            })
            // console.log(this.data.list)
          }
        }
      });
    },
    search() {
      this.getAllList();
    },
    // onStationType() {
    //   this.getAllList();
    // },
    // onType() {
    //   this.getAllList();
    // },
    fresh() {
      this.setData({
        isFresh: true
      })
      this.getAllList();
    },
    onDetail(e: any) {
      console.log(e)
      let type = e.currentTarget.dataset.type
      let id = e.currentTarget.dataset.id
      let warnCode = e.currentTarget.dataset.warncode
      if (warnCode == 0) {
        wx.navigateTo({
          url: '/pages/siteDetail/siteDetail?type=' + type + '&id=' + id
        })
      } else {
        wx.navigateTo({
          url: '/pages/alarmDetail/alarmDetail?type=' + type + '&id=' + id
        })
      }
      // console.log(e)
    }
  },
  //小程序声明周期
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      this.setData({
        comID: wx.getStorageSync("userInfo").data.comID,
        userID: wx.getStorageSync("userInfo").data.userID,
        isHttp:true,
      });
      this.getAllList();
      this.data.intervalId = setInterval(() => {
        this.Timertask();
      }, 10000);
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
      if (this.data.intervalId != null) {
        clearInterval(this.data.intervalId);
        this.setData({
          isHttp:false
        })
      }
    },
  },
  pageLifetimes: {
    show: function () {
      // 页面被展示
      if (!this.data.isHttp) {
        this.setData({
          comID: wx.getStorageSync("userInfo").data.comID,
          userID: wx.getStorageSync("userInfo").data.userID
        });
        this.getAllList();
        this.data.intervalId = setInterval(() => {
          this.Timertask();
        }, 10000);
      }
    },
    hide: function () {
      // 页面被隐藏
      if (this.data.intervalId != null) {
        clearInterval(this.data.intervalId);
        this.setData({
          isHttp:false
        })
      }
    },
    resize: function (size) {
      // 页面尺寸变化
    }
  }
})